﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyX.Message;

namespace MyX.Core
{
    public interface XIFactory
    {
        T Get<T>(Guid id) where T : XEntity, new();
    }

    internal class XFactory : XIFactory
    {
        public T Get<T>(Guid id) where T : XEntity, new()
        {
            XEntity entity;
            if (!_entitys.TryGetValue(id, out entity))
            {
                entity = new T();
                entity.ID = id;
                entity = _entitys.GetOrAdd(id, entity);
                var loadCount = entity.Load(XClient.EventRepository.FindForEntityID(entity.ID));
            }
            return (T)entity;
        }

        static XFactory()
        {
            _entitys = new ConcurrentDictionary<Guid, XEntity>();
        }
        private static readonly ConcurrentDictionary<Guid, XEntity> _entitys;
    }
}
